import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

export default class Goods extends Base {
  static labelDefaultData = ['种草', '好物推荐', '达人分享', '严选品质', '精选热门', '种草', '好物推荐', '达人分享', '严选品质', '精选热门'];

  constructor(goods) {
    super();

    this.id = SystemUtil.getId();
    this.defId = goods.goodsDefId;
    this.name = goods.goodsName;
    this.pic = this.getOnePic(goods.goodsPic);
    this.monthCount = goods.monthTradeCount || 0;

    let score = goods.score;
    score = SystemUtil.floorValue(score / 2);
    this.score = score;

    let labelFields = ['label1', 'label2', 'label3'];
    let labelList = [];
    for (let i = 0; i < labelFields.length; i++) {
      const field = labelFields[i];
      let label = goods[field];
      if (!SystemUtil.isEmpty(label)) {
        labelList.push(label);
      }
    }

    if (labelList.length === 0) {
      labelList = this.getRandomLabelList();
    }
    this.labelList = labelList;

    let distance = goods.distance;
    let distanceText;
    if (distance < 1000) {
      distance = Number(distance.toFixed(1));
      distanceText = `${distance}m`;
    } else {
      distance /= 1000;
      distance = Number(distance.toFixed(1));
      distanceText = `${distance}km`;
    }

    this.distance = distance;
    this.distanceText = distanceText;
  }

  getRandomLabelList() {
    let labelDefaultData = Goods.labelDefaultData;
    let len = labelDefaultData.length;
    len /= 2;

    let id = this.defId;
    let index = id % len;
    let count = id % 2 === 0 ? 2 : 3;

    let labelList = labelDefaultData.slice(index, index + count);
    return labelList;
  }
}
